Skip to content

FIX GCG: compare stripped suffixes and survive an all-rejected candidate batch - #4637

Open
K L Aadithya (Aadithya-kl) wants to merge 11 commits into
microsoft:mainfrom
Aadithya-kl:fix/gcg-candidate-filter-4545-4546
Open

K L Aadithya (Aadithya-kl) wants to merge 11 commits into
microsoft:mainfrom
Aadithya-kl:fix/gcg-candidate-filter-4545-4546

Conversation

@Aadithya-kl

Copy link
Copy Markdown
Contributor

Description

Fixes #4545 and fixes #4546: two bugs in the GCG candidate filter, fixed identically in LengthPreservingFilter.filter_candidates and the legacy MultiPromptAttack.get_filtered_cands so the two stay in parity.

#4547 (> vs >= vocab clamp) is left to its existing PR.

🤖 Generated with Claude Code

Copilot AI balanced review requested due to automatic review settings November 25, 2026 14:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Both candidate filters return a stripped fallback suffix that can re-tokenize differently from the retained control tokens, causing the reported loss to score different token IDs.

1 open issue
What changed in this PR

This PR addresses #4545 and #4546 in PyRIT’s GCG candidate filtering, keeping the default and legacy implementations aligned.

Changes:

  • Compare stripped suffixes to reject unchanged candidates while preserving decoded separator spaces.
  • Fall back to the current control when all candidates are rejected, with a clear legacy error when no fallback is available.
  • Add regression tests covering both filtering paths.
File Description
tests/​unit/​executor/​promptgen/​gcg/​test_default_implementations.py Adds regression and parity tests for unchanged candidates and all-rejected batches.
pyrit/​executor/​promptgen/​gcg/​default_implementations.py Updates suffix comparison and adds fallback padding.
pyrit/​executor/​promptgen/​gcg/​attack/​base/​attack_manager.py Applies equivalent legacy filtering changes and documents the missing-fallback error.

Review effort: Balanced

candidates = candidates + [candidates[-1]] * (len(candidate_tokens) - len(candidates))
# When every row is rejected, fall back to the current suffix so
# the step re-scores it instead of indexing an empty list.
pad = candidates[-1] if candidates else current_control
@Aadithya-kl

Copy link
Copy Markdown
Contributor Author

Addressed the Copilot review (fallback scored stripped text):

  • Added decode_control_tokens in default_implementations.py, used for candidates and for the current control alike.
  • GCGMultiPromptAttack now passes the unstripped decode of the current control ids (instead of the stripped control_str) to the filter, on both the callback path and the proposer path. The no-op comparison still strips both sides, so dropping unchanged rows is unaffected, and an all-rejected batch now pads with a string that re-tokenizes to the current ids.
  • New regression test_all_rejected_fallback_retokenizes_to_current_ids uses a byte-level-style tokenizer where " !" and "!" are different tokens; it fails on the previous commit ([1, 2] vs [2, 2]). Wiring tests updated for the new value.

GCG unit suite: 605 passed.

The keyword-only migration in microsoft#4693 makes control_cand keyword-only;
using the keyword form now keeps these new tests valid in either merge
order.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…ol decode

Mirrors the filter_candidates docstring so the legacy fallback also
re-tokenizes to the current control ids.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants